import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'

// 导入Element Plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

// 导入并全局注册Element Plus图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 导入全局样式
import './assets/main.css'
import './assets/theme.css'

// 导入通用组件
import PageLayout from './components/PageLayout.vue'
import StandardTable from './components/StandardTable.vue'
import SearchToolbar from './components/SearchToolbar.vue'
import StatusTag from './components/StatusTag.vue'
import ActionButtons from './components/ActionButtons.vue'
import FormDialog from './components/FormDialog.vue'

// 创建应用实例
const app = createApp(App)
const pinia = createPinia()

// 全局错误处理
app.config.errorHandler = (err, vm, info) => {
  console.error('Vue全局错误:', err)
  console.error('错误信息:', info)
  console.error('错误组件:', vm?.$options?.name || '未知组件')
}

// 捕获未处理的Promise错误
window.addEventListener('unhandledrejection', event => {
  console.error('未处理的Promise错误:', event.reason)
})

// 全局注册所有Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 使用插件
app.use(ElementPlus, { locale: zhCn })
app.use(router)
app.use(pinia)

// 全局注册组件
app.component('PageLayout', PageLayout)
app.component('StandardTable', StandardTable)
app.component('SearchToolbar', SearchToolbar)
app.component('StatusTag', StatusTag)
app.component('ActionButtons', ActionButtons)
app.component('FormDialog', FormDialog)

// 设置模拟数据标志
localStorage.setItem('useMockData', 'true')
console.log('已启用模拟数据模式')

// 挂载应用
app.mount('#app') 